<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>31.3. zipimport — Import modules from Zip archives &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="31. Importing Modules" href="modules.html" />
    <link rel="next" title="31.4. pkgutil — Package extension utility" href="pkgutil.html" />
    <link rel="prev" title="31.2. imputil — Import utilities" href="imputil.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="pkgutil.html" title="31.4. pkgutil — Package extension utility"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="imputil.html" title="31.2. imputil — Import utilities"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="modules.html" accesskey="U">31. Importing Modules</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-zipimport">
<h1>31.3. <tt class="xref docutils literal"><span class="pre">zipimport</span></tt> &#8212; Import modules from Zip archives<a class="headerlink" href="#module-zipimport" title="Permalink to this headline">¶</a></h1>
<p>
<span class="versionmodified">New in version 2.3.</span></p>
<p>This module adds the ability to import Python modules (<tt class="docutils literal"><span class="pre">*.py</span></tt>,
<tt class="docutils literal"><span class="pre">*.py[co]</span></tt>) and packages from ZIP-format archives. It is usually not
needed to use the <tt class="xref docutils literal"><span class="pre">zipimport</span></tt> module explicitly; it is automatically used
by the builtin <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> mechanism for <tt class="docutils literal"><span class="pre">sys.path</span></tt> items that are paths
to ZIP archives.</p>
<p>Typically, <tt class="docutils literal"><span class="pre">sys.path</span></tt> is a list of directory names as strings.  This module
also allows an item of <tt class="docutils literal"><span class="pre">sys.path</span></tt> to be a string naming a ZIP file archive.
The ZIP archive can contain a subdirectory structure to support package imports,
and a path within the archive can be specified to only import from a
subdirectory.  For example, the path <tt class="docutils literal"><span class="pre">/tmp/example.zip/lib/</span></tt> would only
import from the <tt class="docutils literal"><span class="pre">lib/</span></tt> subdirectory within the archive.</p>
<p>Any files may be present in the ZIP archive, but only files <tt class="docutils literal"><span class="pre">.py</span></tt> and
<tt class="docutils literal"><span class="pre">.py[co]</span></tt> are available for import.  ZIP import of dynamic modules
(<tt class="docutils literal"><span class="pre">.pyd</span></tt>, <tt class="docutils literal"><span class="pre">.so</span></tt>) is disallowed. Note that if an archive only contains
<tt class="docutils literal"><span class="pre">.py</span></tt> files, Python will not attempt to modify the archive by adding the
corresponding <tt class="docutils literal"><span class="pre">.pyc</span></tt> or <tt class="docutils literal"><span class="pre">.pyo</span></tt> file, meaning that if a ZIP archive
doesn&#8217;t contain <tt class="docutils literal"><span class="pre">.pyc</span></tt> files, importing may be rather slow.</p>
<p>Using the built-in <a title="reload" class="reference external" href="functions.html#reload"><tt class="xref docutils literal"><span class="pre">reload()</span></tt></a> function will fail if called on a module
loaded from a ZIP archive; it is unlikely that <a title="reload" class="reference external" href="functions.html#reload"><tt class="xref docutils literal"><span class="pre">reload()</span></tt></a> would be needed,
since this would imply that the ZIP has been altered during runtime.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">PKZIP Application Note</a></dt>
<dd>Documentation on the ZIP file format by Phil Katz, the creator of the format and
algorithms used.</dd>
<dt><span class="target" id="index-742"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0273"><strong>PEP 0273</strong></a> - Import Modules from Zip Archives</dt>
<dd>Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
follows the specification in PEP 273, but uses an implementation written by Just
van Rossum that uses the import hooks described in PEP 302.</dd>
<dt><span class="target" id="index-743"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 0302</strong></a> - New Import Hooks</dt>
<dd>The PEP to add the import hooks that help this module work.</dd>
</dl>
</div>
<p>This module defines an exception:</p>
<dl class="exception">
<dt id="zipimport.ZipImportError">
<em class="property">
exception </em><tt class="descclassname">zipimport.</tt><tt class="descname">ZipImportError</tt><a class="headerlink" href="#zipimport.ZipImportError" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised by zipimporter objects. It&#8217;s a subclass of <a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a>,
so it can be caught as <a title="exceptions.ImportError" class="reference external" href="exceptions.html#exceptions.ImportError"><tt class="xref docutils literal"><span class="pre">ImportError</span></tt></a>, too.</dd></dl>

<div class="section" id="zipimporter-objects">
<span id="id1"></span><h2>31.3.1. zipimporter Objects<a class="headerlink" href="#zipimporter-objects" title="Permalink to this headline">¶</a></h2>
<p><a title="zipimport.zipimporter" class="reference internal" href="#zipimport.zipimporter"><tt class="xref docutils literal"><span class="pre">zipimporter</span></tt></a> is the class for importing ZIP files.</p>
<dl class="class">
<dt id="zipimport.zipimporter">
<em class="property">
class </em><tt class="descclassname">zipimport.</tt><tt class="descname">zipimporter</tt><big>(</big><em>archivepath</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a new zipimporter instance. <em>archivepath</em> must be a path to a ZIP
file, or to a specific path within a ZIP file.  For example, an <em>archivepath</em>
of <tt class="docutils literal"><span class="pre">foo/bar.zip/lib</span></tt> will look for modules in the <tt class="docutils literal"><span class="pre">lib</span></tt> directory
inside the ZIP file <tt class="docutils literal"><span class="pre">foo/bar.zip</span></tt> (provided that it exists).</p>
<p><a title="zipimport.ZipImportError" class="reference internal" href="#zipimport.ZipImportError"><tt class="xref docutils literal"><span class="pre">ZipImportError</span></tt></a> is raised if <em>archivepath</em> doesn&#8217;t point to a valid ZIP
archive.</p>
<dl class="method">
<dt id="zipimport.zipimporter.find_module">
<tt class="descname">find_module</tt><big>(</big><em>fullname</em><span class="optional">[</span>, <em>path</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#zipimport.zipimporter.find_module" title="Permalink to this definition">¶</a></dt>
<dd>Search for a module specified by <em>fullname</em>. <em>fullname</em> must be the fully
qualified (dotted) module name. It returns the zipimporter instance itself
if the module was found, or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> if it wasn&#8217;t. The optional
<em>path</em> argument is ignored&#8212;it&#8217;s there for compatibility with the
importer protocol.</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_code">
<tt class="descname">get_code</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.get_code" title="Permalink to this definition">¶</a></dt>
<dd>Return the code object for the specified module. Raise
<a title="zipimport.ZipImportError" class="reference internal" href="#zipimport.ZipImportError"><tt class="xref docutils literal"><span class="pre">ZipImportError</span></tt></a> if the module couldn&#8217;t be found.</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_data">
<tt class="descname">get_data</tt><big>(</big><em>pathname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.get_data" title="Permalink to this definition">¶</a></dt>
<dd>Return the data associated with <em>pathname</em>. Raise <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> if the
file wasn&#8217;t found.</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.get_source">
<tt class="descname">get_source</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.get_source" title="Permalink to this definition">¶</a></dt>
<dd>Return the source code for the specified module. Raise
<a title="zipimport.ZipImportError" class="reference internal" href="#zipimport.ZipImportError"><tt class="xref docutils literal"><span class="pre">ZipImportError</span></tt></a> if the module couldn&#8217;t be found, return
<a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> if the archive does contain the module, but has no source
for it.</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.is_package">
<tt class="descname">is_package</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.is_package" title="Permalink to this definition">¶</a></dt>
<dd>Return True if the module specified by <em>fullname</em> is a package. Raise
<a title="zipimport.ZipImportError" class="reference internal" href="#zipimport.ZipImportError"><tt class="xref docutils literal"><span class="pre">ZipImportError</span></tt></a> if the module couldn&#8217;t be found.</dd></dl>

<dl class="method">
<dt id="zipimport.zipimporter.load_module">
<tt class="descname">load_module</tt><big>(</big><em>fullname</em><big>)</big><a class="headerlink" href="#zipimport.zipimporter.load_module" title="Permalink to this definition">¶</a></dt>
<dd>Load the module specified by <em>fullname</em>. <em>fullname</em> must be the fully
qualified (dotted) module name. It returns the imported module, or raises
<a title="zipimport.ZipImportError" class="reference internal" href="#zipimport.ZipImportError"><tt class="xref docutils literal"><span class="pre">ZipImportError</span></tt></a> if it wasn&#8217;t found.</dd></dl>

<dl class="attribute">
<dt id="zipimport.zipimporter.archive">
<tt class="descname">archive</tt><a class="headerlink" href="#zipimport.zipimporter.archive" title="Permalink to this definition">¶</a></dt>
<dd>The file name of the importer&#8217;s associated ZIP file, without a possible
subpath.</dd></dl>

<dl class="attribute">
<dt id="zipimport.zipimporter.prefix">
<tt class="descname">prefix</tt><a class="headerlink" href="#zipimport.zipimporter.prefix" title="Permalink to this definition">¶</a></dt>
<dd>The subpath within the ZIP file where modules are searched.  This is the
empty string for zipimporter objects which point to the root of the ZIP
file.</dd></dl>

<p>The <a title="zipimport.zipimporter.archive" class="reference internal" href="#zipimport.zipimporter.archive"><tt class="xref docutils literal"><span class="pre">archive</span></tt></a> and <a title="zipimport.zipimporter.prefix" class="reference internal" href="#zipimport.zipimporter.prefix"><tt class="xref docutils literal"><span class="pre">prefix</span></tt></a> attributes, when combined with a
slash, equal the original <em>archivepath</em> argument given to the
<a title="zipimport.zipimporter" class="reference internal" href="#zipimport.zipimporter"><tt class="xref docutils literal"><span class="pre">zipimporter</span></tt></a> constructor.</p>
</dd></dl>

</div>
<div class="section" id="examples">
<span id="zipimport-examples"></span><h2>31.3.2. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>Here is an example that imports a module from a ZIP archive - note that the
<tt class="xref docutils literal"><span class="pre">zipimport</span></tt> module is not explicitly used.</p>
<div class="highlight-python"><pre>$ unzip -l /tmp/example.zip
Archive:  /tmp/example.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
     8467  11-26-02 22:30   jwzthreading.py
 --------                   -------
     8467                   1 file
$ ./python
Python 2.3 (#1, Aug 1 2003, 19:54:32)
&gt;&gt;&gt; import sys
&gt;&gt;&gt; sys.path.insert(0, '/tmp/example.zip')  # Add .zip file to front of path
&gt;&gt;&gt; import jwzthreading
&gt;&gt;&gt; jwzthreading.__file__
'/tmp/example.zip/jwzthreading.py'</pre>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">31.3. <tt class="docutils literal"><span class="pre">zipimport</span></tt> &#8212; Import modules from Zip archives</a><ul>
<li><a class="reference external" href="#zipimporter-objects">31.3.1. zipimporter Objects</a></li>
<li><a class="reference external" href="#examples">31.3.2. Examples</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="imputil.html"
                                  title="previous chapter">31.2. <tt class="docutils literal docutils literal docutils literal"><span class="pre">imputil</span></tt> &#8212; Import utilities</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="pkgutil.html"
                                  title="next chapter">31.4. <tt class="docutils literal docutils literal docutils literal"><span class="pre">pkgutil</span></tt> &#8212; Package extension utility</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/zipimport.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="pkgutil.html" title="31.4. pkgutil — Package extension utility"
             >next</a> |</li>
        <li class="right" >
          <a href="imputil.html" title="31.2. imputil — Import utilities"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="modules.html" >31. Importing Modules</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>